package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.BVehicleBrandService;
import cn.huiyunche.base.service.mappers.BVehicleBrandMapper;
import cn.huiyunche.base.service.model.BVehicleBrand;
import cn.huiyunche.base.service.model.BVehicleBrandExample;
import cn.huiyunche.base.service.model.BVehicleBrandExample.Criteria;
import cn.huiyunche.base.service.vo.BVeBrandVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌接口实现类
 *
 * @author Administrator
 */
@Service
public class BVehicleBrandServiceImpl implements BVehicleBrandService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BVehicleBrandServiceImpl.class);

    @Autowired
    private BVehicleBrandMapper bVehicleBrandMapper = null;

    public BVehicleBrandMapper getBVehicleBrandMapper() {
        return this.bVehicleBrandMapper;
    }

    @Override
    public BVehicleBrand selectById(Long brandId) {
        LOGGER.info("selectById param : {}", brandId);
        try {
            return this.getBVehicleBrandMapper().selectByPrimaryKey(brandId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, Object> getList(PageVo pageVo, BVehicleBrand bVehicleBrand) {
        LOGGER.info("select brand list params pageVo : {}, bVehicleBrand : {}.", pageVo, bVehicleBrand);
        try {
            Map<String, Object> map = new HashMap<>();
            BVehicleBrandExample example = new BVehicleBrandExample();
            example.setLimitStart(pageVo.getStartIndex());
            example.setLimitEnd(pageVo.getPageSize());
            if (StringUtils.isNotBlank(pageVo.getOrder())) {
                example.setOrderByClause(pageVo.getOrder());
            } else {
                example.setOrderByClause("id DESC");
            }
            String brandPrefix = bVehicleBrand.getBrandPrefix();
            Criteria createCriteria = example.createCriteria();
            if (StringUtils.isNotBlank(brandPrefix)) {
                createCriteria.andBrandPrefixEqualTo(brandPrefix);
            }
            String brandName = bVehicleBrand.getBrandName();
            if (StringUtils.isNotBlank(brandName)) {
                createCriteria.andBrandNameEqualTo(brandName);
            }
            String brandNation = bVehicleBrand.getBrandNation();
            if (StringUtils.isNotBlank(brandNation)) {
                createCriteria.andBrandNationEqualTo(brandNation);
            }
            LOGGER.info("select brand list params pageVo : {}, bVehicleBrand : {}.", pageVo, bVehicleBrand);
            List<BVeBrandVo> list2 = new ArrayList<>();
            pageVo.setTotalRecord(this.getBVehicleBrandMapper().countByExample(example));
            List<BVehicleBrand> list = this.getBVehicleBrandMapper().selectByExample(example);
            for (BVehicleBrand brand : list) {
                BVeBrandVo brandVo = new BVeBrandVo();
                BeanUtils.copyProperties(brand, brandVo);
                String url = QiniuUtils.generateDownloadTicket(HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET), brand.getBrandLogo(), "", "", "");
                brandVo.setUrl(url);

                list2.add(brandVo);
            }

            map.put("page", pageVo);
            map.put("brands", list2);
            return map;
        } catch (Exception e) {
            LOGGER.error("select brand list error : {}.", e);
            throw new BusinessException("查询品牌异常");
        }
    }

    @Override
    public int editBrand(BVehicleBrand bVehicleBrand) {
        LOGGER.info("update brand params bVehicleBrand : {}.", bVehicleBrand);
        try {
            Long id = bVehicleBrand.getId();
            if (id != null) {
                return this.getBVehicleBrandMapper().updateByPrimaryKey(bVehicleBrand);
            } else {
                return this.getBVehicleBrandMapper().insertSelective(bVehicleBrand);
            }
        } catch (Exception e) {
            LOGGER.error("update brand error : {}.", e);
            throw new BusinessException("修改品牌信息异常");
        }
    }

}